package com.zjw.touchscreen.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;

public class DatabaseHelper extends SQLiteOpenHelper {

    private static DatabaseHelper databaseHelper;

    private DatabaseHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    public static DatabaseHelper getInstance(@Nullable Context context, @Nullable String name, int version) {
        if (databaseHelper == null) {
            synchronized (DatabaseHelper.class) {
                if (databaseHelper == null) {
                    databaseHelper = new DatabaseHelper(context, name, null, version);
                }
            }
        }
        return databaseHelper;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        //创建数据库完成调用一次
        String SQL_CREATE_ENTRIES = "CREATE TABLE IF NOT EXISTS user (" +
                "`id` INTEGER PRIMARY KEY AUTOINCREMENT," +
                "`name` VARCHAR(30) NOT NULL," +
                "`pwd` VARCHAR(30) NOT NULL," +
                "`token` VARCHAR(255) NOT NULL" +
                ");";
        db.execSQL(SQL_CREATE_ENTRIES);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //版本升级时调用
    }
}
